<?php

namespace app\admin\controller;

use app\common\controller\Backend;

use think\Controller;
use think\Db;
use think\Log;
use think\Request;

/**
 * 
 *
 * @icon user
 */
class Userinfo extends Backend
{
    
    /**
     * User模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');

    }

    public function index()
    {
        //设置过滤方法
        //$this->request->filter(['strip_tags', 'htmlspecialchars']);
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach($list as $key=>$val){

                $userinfo = Db::name('user_info')->where('userId',$val['userId'])->find();
                if($userinfo){
                    $list[$key]['qq']   = $userinfo['qq'];
                    $list[$key]['mobile']   = $userinfo['mobile'];
                    $list[$key]['youji']   = $userinfo['province'].$userinfo['city'].$userinfo['district'].$userinfo['address'];
                    $list[$key]['suozai']   = $userinfo['province2'].$userinfo['city2'].$userinfo['district2'].$userinfo['address2'];
                }else{
                    $list[$key]['qq']   = '';
                    $list[$key]['mobile']   = '';
                    $list[$key]['youji']   = '';
                    $list[$key]['suozai']   = '';
                }

            }


            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        $promotion = Db::name('sms_code')->where('phone',$row['phone'])->find();
        $row['appShop'] = $promotion['appShop'];
        $row['promotion'] = $promotion['promotion'];
        $bank = Db::name('user_bank')->where(['userId'=>$row['userId'],'isMain'=>1])->find();
        $row['idCards'] = $bank['bankCard'];
        $row['bank'] = $bank['bank'];
        $row['createTime1'] = $bank['createTime'];
        $userinfo = Db::name('user_info')->where(['userId'=>$row['userId']])->find();
        $row['province2'] = $userinfo['province2'];
        $row['city2'] = $userinfo['city2'];
        $row['district2'] = $userinfo['district2'];
        $row['address2'] = $userinfo['address2'];
        $row['wangwang'] = $userinfo['wangwang'];
        $row['tel'] = $userinfo['tel'];
        $row['qq'] = $userinfo['qq'];

        if (!$row)
            $this->error(__('No Results were found'));
        if ($this->request->isPost())
        {
            $this->code = -1;
            $params = $this->request->post("row/a");
            if ($params)
            {
                    $data['email'] = $params['email'];
                    $data1['qq'] = $params['qq'];
                    $data1['wangwang'] = $params['wangwang'];
                    $data1['province2'] = $params['province'];
                    $data1['city2'] = $params['city'];
                    $data1['district2'] = $params['district'];
                    $data1['address2'] = $params['address'];
                    $data1['tel'] = $params['tel'];
                    $data1['userId'] = $row['userId'];

                    $result = Db::name('user')->where('userId',$row['userId'])->update($data);
                    $userinfo = Db::name('user_info')->where(['userId'=>$row['userId']])->find();
                    if($userinfo){
                        Db::name('user_info')->where('userId',$row['userId'])->update($data1);
                    }else{
                        Db::name('user_info')->insert($data1);
                    }
                    if ($result !== false)
                    {
                        $this->code = 1;
                    }
                    else
                    {
                        $this->msg = $row->getError();
                    }
            }
            else
            {
                $this->msg = __('Parameter %s can not be empty', '');
            }

            return;
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }


    public function detail($ids = NULL)
    {
        $row = $this->model->get($ids);
        $promotion = Db::name('sms_code')->where('phone',$row['phone'])->find();
        $row['appShop'] = $promotion['device'];
        $row['promotion'] = $promotion['promotion'];
        $bank = Db::name('user_bank')->where(['userId'=>$row['userId'],'isMain'=>1])->find();
        $row['idCards'] = $bank['idCard'];
        $row['bank'] = $bank['bank'];
        $row['createTime1'] = $bank['createTime'];

        $userinfo = Db::name('user_info')->where(['userId'=>$row['userId']])->find();
        $row['province2'] = $userinfo['province2'];
        $row['city2'] = $userinfo['city2'];
        $row['district2'] = $userinfo['district2'];
        $row['address2'] = $userinfo['address2'];
        $row['wangwang'] = $userinfo['wangwang'];
        $row['tel'] = $userinfo['tel'];
        $row['qq'] = $userinfo['qq'];
        if (!$row)
            $this->error(__('No Results were found'));
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
    

}
